package com.user.action;



import com.opensymphony.xwork2.ActionContext;
import com.user.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.user.service.UserService;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.Map;

@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>,SessionAware {
    private Map<String,Object> session = null;
    private User user = null;
    @Autowired
    private UserService userService = null;
    public User getModel() {
        this.user = new User();
        return this.user;
    }

    public void setSession(Map<String, Object> map) {
        this.session = map;
    }

    public String loginView() {
        return "loginViewSuccess";
    }

    public String landing(){
        boolean flag = userService.enter(user);
        if(flag){
            session.put("user",user);
            return "landingSuccess";
        }else{
            return "landingError";
        }
    }

    public String login(){
        boolean flag = userService.addUser(user);
        if(flag){
            return "loginSuccess";
        }else{
            return "loginError";
        }
    }

    public String select(){
        User user1=userService.selectUser(user.getuId());
        ActionContext.getContext().getValueStack().set("user",user1);
        return "selectSuccess";
    }

    public String update(){
        boolean flag = userService.updateUser(user);
        if(flag){
            return "updateSuccess";
        }else{
            return "updateError";
        }
    }
}
